package com.jinghangzz.shop.orders.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jinghangzz.shop.common.util.ConstatFinalUtil;
import com.jinghangzz.shop.common.util.PageInfoUtil;
import com.jinghangzz.shop.goods.pojo.AGoods;
import com.jinghangzz.shop.goods.service.IGoodsService;
import com.jinghangzz.shop.orders.dao.IAOrdersDao;
import com.jinghangzz.shop.orders.dao.IAOrdersGoodsDao;
import com.jinghangzz.shop.orders.dao.IAOrdersHistoryDao;
import com.jinghangzz.shop.orders.pojo.AOrders;
import com.jinghangzz.shop.orders.pojo.AOrdersGoods;
import com.jinghangzz.shop.orders.pojo.AOrdersHistory;
import com.jinghangzz.shop.orders.service.IOrdersService;
import com.jinghangzz.shop.users.pojo.AUsersAddress;
import com.jinghangzz.shop.users.pojo.AUsersCar;
import com.jinghangzz.shop.users.service.IUsersService;

/**
 * 订单的Service类
 * @author wangshSxt
 *
 */
@Service("ordersService")
public class OrdersServiceImpl implements IOrdersService
{
	@Resource
	private IAOrdersDao ordersDao ; 
	@Resource
	private IAOrdersGoodsDao ordersGoodsDao;
	@Resource
	private IAOrdersHistoryDao ordersHistoryDao;
	
	@Resource
	private IGoodsService goodsService;
	@Resource
	private IUsersService usersService;
	
	@Override
	public AOrders findOneOrdersService(Map<String, Object> condMap)
	{
		return this.ordersDao.findOneDao(condMap);
	}
	
	@Override
	public JSONObject saveOneOrdersService(AOrders orders)
	{
		Map<String, Object> condMap = new HashMap<String, Object>();
		JSONObject resultJSON = new JSONObject();
		
		/* 查询用户选择的收货地址id,查询地址表 */
		condMap.clear();
		condMap.put("id", orders.getAddressId());
		AUsersAddress usersAddress = this.usersService.findOneUsersAddressService(condMap);
		
		orders.setContact(usersAddress.getContact());
		orders.setPhone(usersAddress.getPhone());
		orders.setAddress(usersAddress.getAddress());
		orders.setZipCode(usersAddress.getZipCode());
		
		orders.setCreateTime(new Date());
		orders.setUpdateTime(new Date());
		orders.setPubTime(new Date());
		
		
		/* 影响条数 */
		int res = this.ordersDao.saveOne(orders);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			
			/* 要把购物车买的商品给删除 */
			
			/* 总价格 */
			double totalPrice = 0 ; 
			/* 总重量 */
			double totalWeight = 0 ; 
			/* 因为只有保存了订单,才会有订单id */
			for (Iterator iterator = orders.getOrdersGoodList().iterator(); iterator.hasNext();)
			{
				AOrdersGoods ordersGoods = (AOrdersGoods) iterator.next();
				
				ordersGoods.setOrdersId(orders.getId());
				
				int goodsId = ordersGoods.getGoodsId() ; 
				/* 根据商品id查询商品明细 */
				condMap.put("id", goodsId);
				AGoods goods = this.goodsService.findOneGoodsService(condMap);
				/* 将商品的一些信息设置到ordersGoods(瞬时存储) */
				ordersGoods.setName(goods.getName());
				ordersGoods.setPrice(goods.getPrice());
				ordersGoods.setWeight(goods.getWeight());
				ordersGoods.setTotalPrice(ordersGoods.getPrice() * ordersGoods.getNum());
				
				ordersGoods.setStatus(Byte.valueOf("1"));
				/* 设置一些时间 */
				ordersGoods.setCreateTime(new Date());
				ordersGoods.setUpdateTime(new Date());
				
				/* 存储订单明细(商品) */
				this.saveOneOrdersGoodsService(ordersGoods);
				
				totalPrice = totalPrice + ordersGoods.getTotalPrice() ; 
				totalWeight = totalWeight + ordersGoods.getWeight() * ordersGoods.getNum() ; 
				
				/* 根据用户id和商品id,查询购物车表;如果查询到,直接干掉 */
				condMap.clear();
				condMap.put("usersId", orders.getUsersId());
				condMap.put("goodsId", goods.getId());
				AUsersCar usersCar = this.usersService.findOneUsersCarService(condMap);
				/* 购物车对应的商品删除 */
				this.usersService.deleteOneUsersCarService(usersCar);
			}
			
			/* 订单操作历史 */
			AOrdersHistory ordersHistory = new AOrdersHistory();
			ordersHistory.setSn(UUID.randomUUID().toString());
			ordersHistory.setOrdersId(orders.getId());
			ordersHistory.setContent("您提交了订单，请等待系统确认!");
			ordersHistory.setStatus(Byte.valueOf("1"));
			ordersHistory.setOrdersStatus(orders.getStatus());
			ordersHistory.setCreateTime(new Date());
			this.saveOneOrdersHistoryService(ordersHistory);
			
			/* 需要更新 */
			orders.setTotalPrice(totalPrice);
			orders.setTotalWeight(totalWeight);
			this.updateOneOrdersService(orders);
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", orders.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}
	
	@Override
	public JSONObject updateOneOrdersService(AOrders orders)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.ordersDao.updateOne(orders);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", orders.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneOrdersService(AOrders orders)
	{
		return null ;
	}
	
	@Override
	public List findCondListOrdersService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		Map<String, Object> resultMap = new HashMap<String, Object>();
		/* 为关键字加上%,实现模糊匹配 
		 * */
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword")  + "%");
		}
		
		if(pageInfoUtil != null)
		{
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			/* 查询总记录数 */
			List<AOrders> roleList = this.ordersDao.findCondList(condMap);
			return roleList;
		}
		/* 不分页查询数据 */
		List<AOrders> roleList = this.ordersDao.findCondList(condMap);
		return roleList;
	}
	
	@Override
	public AOrdersGoods findOneOrdersGoodsService(Map<String, Object> condMap)
	{
		return this.ordersGoodsDao.findOneDao(condMap);
	}
	
	@Override
	public JSONObject saveOneOrdersGoodsService(AOrdersGoods ordersGoods)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.ordersGoodsDao.saveOne(ordersGoods);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", ordersGoods.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}
	
	@Override
	public JSONObject updateOneOrdersGoodsService(AOrdersGoods ordersGoods)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.ordersGoodsDao.updateOne(ordersGoods);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", ordersGoods.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneOrdersGoodsService(AOrdersGoods ordersGoods)
	{
		return null ; 
	}
	
	@Override
	public List findCondListOrdersGoodsService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		Map<String, Object> resultMap = new HashMap<String, Object>();
		/* 为关键字加上%,实现模糊匹配 
		 * */
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword")  + "%");
		}
		
		if(pageInfoUtil != null)
		{
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			/* 查询总记录数 */
			List<AOrdersGoods> roleList = this.ordersGoodsDao.findCondList(condMap);
			return roleList;
		}
		/* 不分页查询数据 */
		List<AOrdersGoods> roleList = this.ordersGoodsDao.findCondList(condMap);
		return roleList;
	}
	
	@Override
	public AOrdersHistory findOneOrdersHistoryService(Map<String, Object> condMap)
	{
		return this.ordersHistoryDao.findOneDao(condMap);
	}
	
	@Override
	public JSONObject saveOneOrdersHistoryService(AOrdersHistory ordersHistory)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.ordersHistoryDao.saveOne(ordersHistory);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", ordersHistory.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}
	
	@Override
	public JSONObject updateOneOrdersHistoryService(AOrdersHistory ordersHistory)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.ordersHistoryDao.updateOne(ordersHistory);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", ordersHistory.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneOrdersHistoryService(AOrdersHistory ordersHistory)
	{
		return null ; 
	}
	
	@Override
	public List findCondListOrdersHistoryService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{

		Map<String, Object> resultMap = new HashMap<String, Object>();
		/* 为关键字加上%,实现模糊匹配 
		 * */
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword")  + "%");
		}
		
		if(pageInfoUtil != null)
		{
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			/* 查询总记录数 */
			List<AOrdersHistory> roleList = this.ordersHistoryDao.findCondList(condMap);
			return roleList;
		}
		/* 不分页查询数据 */
		List<AOrdersHistory> roleList = this.ordersHistoryDao.findCondList(condMap);
		return roleList;
	}
}
